package com.mouliu.base.syn005;

/**
 * synchronized异常 会释放锁
 * @author liu.mou
 */
public class SyncException {
    private int i = 0;

    public synchronized  void operation(){
        while (true){
            try {
                i++;
                Thread.sleep(100);
                System.out.println(Thread.currentThread().getName() + " , i = " + i);
                if (i==20){
                    throw new RuntimeException();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public synchronized void operation02(){
        System.out.println("kkkkkk");
    }
    public static void main(String[] args){
        final SyncException se = new SyncException();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                se.operation();
            }
        },"t1");
        t1.start();
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                se.operation02();
            }
        },"t2");
        t2.start();

    }
}
